
import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";
import { Col, Form, Input, Modal, Row, Select, InputNumber, Flex, Radio, Spin, TreeSelect, message } from "antd";
import React, { useState, useEffect, useRef, useImperativeHandle, } from "react";
import { classApi } from "@/api";const ClassTypeForm = ({ treeData = [], onRef, getList = Function }) => {
    const [show, setShow] = useState(false);
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();


    /**@description 表单布局 */
    const formItemLayout = {
        labelCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 4,
            },
        },
        wrapperCol: {
            xs: {
                span: 24,
            },
            sm: {
                span: 20,
            },
        },
    };

    /**@description 取消 */
    const onCanle = () => {
        setShow(false);
    }

    /**@description 添加|修改操作 */
    const open = async (type = String || undefined, record) => {
        const { id = undefined, parentId = undefined } = record;
        setShow(true);
        form.resetFields();
        form.setFieldValue("type", "action." + type)




        if (type === "update") {
            parentId && form.setFieldValue("parentId", parentId);

            if (id) {
                try {
                    setLoading(true);
                    const data = await classApi.getClass(id);
                    console.log("加载到详情：", data)
                    form.setFieldsValue({
                        parentId: parentId || undefined,
                        ...data
                    });


                } catch (error) {

                } finally {
                    setLoading(false);
                }
            }
        } else if (type === "add") {
            // // 新增 
            // form.setFieldValue("parentId", id);
            form.setFieldsValue({
                "parentId": id,
            })
        } else {
            form.setFieldValue("parentId", "0");
        }


    }

    /**@description 异步保存数据 */
    const createClass = async (value) => {

        try {
            const data = await classApi.createClass(value);
            getList();
            onCanle();
        } catch (error) {

        } finally {

        }

    }

    /**@description 校验表单 */
    const validateForm = async () => {
        form.validateFields().then(res => {
            createClass(res)
        })
    }

    /**@description 向外暴露方法 */
    useImperativeHandle(onRef, () => ({
        open
    }))

    /**@description form表单props配置 */
    const formProps = {
        form,
        initData: {
            type: undefined,
            parentId: "0",
        },
        rules: {
            parentId: [{ required: true, message: '上级类目为必选项' }],
            className: [{ required: true, message: '菜单名称不能为空' }],
            status: [{ required: true, message: '状态选项为必选项' }],
        },
    }

    useEffect(() => {
        !show && form.resetFields()
    }, [show])



    return (
        <>
            <Modal
                title="类目编辑"
                width={550}
                open={show}
                centered={true}
                destroyOnClose={true}
                onCancel={onCanle}
                onOk={validateForm}
            >
                <Spin spinning={loading}>
                    <Form
                        {...formItemLayout}
                        {...formProps}
                    >
                        <Row gutter={24}>
                            <Col span={24} style={{ display: "none" }} >
                                <Form.Item label="id" name="id">
                                    <Input />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item rules={formProps.rules["parentId"]} label="上级类目" name="parentId">
                                    <TreeSelect
                                        treeData={treeData}
                                        fieldNames={{
                                            label: "className",
                                            value: "id"
                                        }}
                                    />

                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item
                                    label="类目名称"
                                    rules={formProps.rules["className"]}
                                    name="className">
                                    <Input placeholder="请输入类目名称" />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="显示排序" name="sort">
                                    <InputNumber />
                                </Form.Item>
                            </Col>
                            <Col span={24}>
                                <Form.Item label="状态"
                                    rules={formProps.rules["status"]}
                                    name="status">
                                    <Radio.Group
                                        block
                                        options={getIntDictOptions(DICT_TYPE.COMMON_STATUS)}
                                        defaultValue="dir"
                                        optionType="button"
                                        buttonStyle="solid"
                                    />
                                </Form.Item>
                            </Col>
                        </Row>
                    </Form>
                </Spin>

            </Modal>
        </>
    )
}
export default ClassTypeForm;